package com.xiangyang.service.impl;

import com.xiangyang.entity.Supplier;
import com.xiangyang.enums.UpdateResultStatus;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.mapper.ISupplierMapper;
import com.xiangyang.mapper.InventoryMapper;
import com.xiangyang.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class SupplierServiceimpl implements ISupplierService {
    @Autowired
    private ISupplierMapper supplierMapper;
    @Autowired
    private InventoryMapper inventoryMapper;


    @Override
//    @OperationLog(title = "删除供应商", businessType = "删除" )
    public boolean deleteVendor(Long vendorId) {
        int vendorIds = supplierMapper.deleteDrugVendor(vendorId);
        if(vendorIds>0){
            return true;
        }
        return false;
    }


    @Transactional
    @Override
//    @OperationLog(title = "修改供应商", businessType = "修改",oldDataMethod = "getOldData", newDataMethod = "getNewData" )
     public UpdateResultStatus updateVendor(Long vendorId) {
        int updateDrugs = supplierMapper.deleteDrugVendor(vendorId);
        if (updateDrugs >0) {
            boolean inventoryUpdated = updateInventoryForVendor(vendorId);
            if (inventoryUpdated) {
                return UpdateResultStatus.SUCCESS_WITH_INVENTORY_UPDATED;
            } else {
                return UpdateResultStatus.SUCCESS_BUT_INVENTORY_UPDATE_FAILED;
            }
        }
        return UpdateResultStatus.FAILURE;
    }


      private boolean updateInventoryForVendor(Long vendorId) {
        try {
            Result inventoryResult = (Result) inventoryMapper.updateInventoryForVendor(vendorId);
            return inventoryResult.isSuccess();
        } catch (Exception e) {
            return false;
        }
    }
    @Override
    public Result selectVendor(Supplier Supplier) {
        return Result.ok(supplierMapper.selectDrugVendor());


    }
    @Override
//    @OperationLog(title = "添加供应商", businessType = "添加")
    public String insertVendor(Supplier supplier) {
        int insertDrug = supplierMapper.insertDrugVendor(supplier);
        if (insertDrug!=1) {
            return "添加失败";
        }
        return "添加成功";
    }
}


    //笨方法
//        Result updatedrugs = supplierMapper.UpdateDrugVendor(vendorId);
//        if(vendorId==1){
//            if (updatedrugs.isSuccess()) {
//                // 更新与该供应商关联的库存信息
//                boolean inventoryUpdated = updateInventoryForVendor(vendorId);
//                if (inventoryUpdated) {
////                    return "修改成功，库存信息也已更新";
//                } else {
////                    return "修改成功，但库存信息更新失败";
//                }
//            }
//        }
//        return null;
    //笨方法
//        if(vendorId!=null&&vendorId>0){
//            return true;
//        }
//        return false;